package com.example.reporter;

import java.util.ArrayList;
import java.util.Map;

import com.example.aggregator.Aggregator;
import com.example.aggregator.RequestStat;
import com.example.collector.RequestInfo;
import com.example.storage.MetricsStorage;
import com.example.viewer.Viewer;

public class ScheduledReporter {

    private MetricsStorage storage;
    private Aggregator aggregator;
    private Viewer viewer;

    public ScheduledReporter(MetricsStorage storage, Aggregator aggregator, Viewer viewer) {
        this.storage = storage;
        this.aggregator = aggregator;
        this.viewer = viewer;
    }

    protected void doStatAndReport(double startTimeInMillis, double endTimeInMillis) {

        Map<String, ArrayList<RequestInfo>> requestDatas = this.storage.getDatas(startTimeInMillis, endTimeInMillis);

        double durationInSection = (endTimeInMillis - startTimeInMillis) / 1000;
        Map<String, RequestStat> requestStats = this.aggregator.aggregate(requestDatas, durationInSection);
        this.viewer.output(startTimeInMillis, endTimeInMillis, requestStats);
    }
}
